package com.yingpai.fitness.dvp_2_0.common.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateFormatUtil {

    static final String TAG = DateFormatUtil.class.getSimpleName();

    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYPMMPDD = "yyyy. MM. dd";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String HH_MM = "HH:mm";
    public static final String HH_MM_SS = "HH:mm:ss";

    /**
     * 获得指定日期的前一天
     *
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay) {//可以用new Date().toLocalString()传递参数  
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c
                .getTime());
        return dayBefore;
    }

    /**
     * 获得指定日期的后一天
     *
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
                .format(c.getTime());
        return dayAfter;
    }

    // 钱一天的日期
    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }

    //
    /*public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, -1);  
        date = calendar.getTime();  
        return date;  
    }  */
    public static String getTimes(long timeStamp) {
        SimpleDateFormat format = new SimpleDateFormat(HH_MM_SS);
        Date date = new Date(timeStamp);
        String strdate = format.format(date);
        return strdate;

    }

    /**
     * 格式时期
     *
     * @param date
     * @param format
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String format(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取今天的日期
     *
     * @return
     */
    public static Date today() {
        return new Date();
    }

    /*
     * 获取今天的字符串日期
     * */
    public static String getTodaydate() {
        long time = System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date d1 = new Date(time);
        String t1 = format.format(d1);
        return t1;
    }

    public static String getCurrentDate() {
        long time = System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        Date d1 = new Date(time);
        String t1 = format.format(d1);
        return t1;
    }

    public static String getTodayshortdate() {
        long time = System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format = new SimpleDateFormat(YYYYMMDD);
        Date d1 = new Date(time);
        String t1 = format.format(d1);
        return t1;
    }

    /**
     * //判断是否是今天
     *
     * @param dateStr
     * @return
     */
    public static boolean isToday(String dateStr) {
        if (TextUtils.isEmpty(dateStr)) {
            return true;
        }
        Calendar calendarToday = Calendar.getInstance();
        calendarToday.setTime(today());
        Calendar calendardateStr = Calendar.getInstance();
        calendardateStr.setTime(parseDate(dateStr, YYYY_MM_DD));
        if (calendardateStr.get(Calendar.YEAR) == calendarToday.get(Calendar.YEAR)
                && calendardateStr.get(Calendar.MONTH) == calendarToday.get(Calendar.MONTH)
                && calendardateStr.get(Calendar.DAY_OF_WEEK) == calendarToday.get(Calendar.DAY_OF_WEEK)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否为昨天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsYesterday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsToday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }


    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS, Locale.CHINA));
        }
        return DateLocal.get();
    }

    /**
     * //判断是否是今年
     *
     * @param dateStr
     * @return
     */
    public static boolean isThisYear(String dateStr) {
        if (TextUtils.isEmpty(dateStr)) {
            return true;
        }
        Calendar calendarToday = Calendar.getInstance();
        calendarToday.setTime(today());
        Calendar calendardateStr = Calendar.getInstance();
        calendardateStr.setTime(parseDate(dateStr, YYYY_MM_DD));
        if (calendardateStr.get(Calendar.YEAR) == calendarToday.get(Calendar.YEAR)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 用指定的格式字符串
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date parseDate(String dateStr, String format) {
        try {
            Date date = new SimpleDateFormat(format).parse(dateStr);
            return date;
        } catch (ParseException e) {
            // ignore
        }
        return null;
    }

    public static Calendar date2Calendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static String formartTickTime1(long tickTime) {
        if (tickTime <= 0) {
            return "正在开奖";
        }
        int day = (int) (tickTime / (60 * 60 * 24));
        tickTime -= day * (60 * 60 * 24);
        int hour = (int) (tickTime / (60 * 60));
        tickTime -= hour * 60 * 60;
        int minute = (int) (tickTime / 60);
        int second = (int) (tickTime - minute * 60);
        String format = null;
        if (day > 0) {
//			if(minute<10){
//				format = day+ "天"+hour + "小时0" + minute + "分钟";
//			}
//			else{
            format = day + "天" + hour + "小时";
//			}
        } else if (hour > 0) {
            if (hour < 10 && minute >= 10) {
                format = hour + "小时" + minute + "分";
            } else if (hour < 10 && minute < 10) {
//				format ="0"+ hour + "小时0" + minute + "分";
                //去掉小时前面的0
                format = hour + "小时0" + minute + "分";
            } else if (hour > 10 && minute < 10) {
                format = hour + "小时0" + minute + "分";
            } else {
                format = hour + "小时" + minute + "分";
            }
        } else if (minute > 0) {
            format = formatNumber(minute) + "分" + formatNumber(second) + "秒";
        } else {
            format = formatNumber(second) + "秒";
        }
        return format;
    }

    public static String formatTickTime2(long tickTime) {
        int day = (int) (tickTime / (60 * 60 * 24));

        tickTime -= day * (60 * 60 * 24);
        int hour = (int) (tickTime / (60 * 60));

        tickTime -= hour * 60 * 60;
        int minute = (int) (tickTime / 60);

        int second = (int) (tickTime - minute * 60);

        String format = null;
        if (day > 0) {
            format = day + "天" + hour + "小时" + minute + "分钟";
        } else if (hour > 0) {
            format = hour + "小时" + minute + "分钟";
        } else {
            format = formatNumber(minute) + ":" + formatNumber(second) + "";
        }
        return format;
    }

    private static String formatNumber(int num) {
        if (num < 10) {
            return "0" + num;
        }
        return String.valueOf(num);
    }

    public static String formatNewsDate(String str_date) {
        //  2015-08-14 07:38:27
        long time = parseDate(str_date, YYYY_MM_DD_HH_MM_SS).getTime();  //  1439509107000

        long current_day = new Date().getTime();
        Long reduce_time = (current_day - time) / 1000;
        if (reduce_time < 24 * 60 * 60) {
            //  昨天
            long hour = reduce_time / 3600;
            if (hour == 0) {
                return "刚刚";
            } else if (hour > 0) {
                return hour + "小时前";
            }

        } else if (48 * 60 * 60 > reduce_time && reduce_time > 24 * 60 * 60) {
            //今天
            String[] split = str_date.trim().split(" ");
            String substring = split[1].substring(0, split[1].lastIndexOf(":"));
            return "昨天 " + substring;
        } else if (reduce_time > 30 * 60 * 60) {
            String[] split = str_date.trim().split(" ");
            String substring = split[1].substring(0, split[1].lastIndexOf(":"));

            String substring2 = split[0].substring(split[0].indexOf("-") + 1);

            return substring2 + " " + substring;
        }

/*		// 1439537613892
        long yesterday = new Date().getTime()-24*60*60*1000;   //1439451231684
		if(yesterday>time){
			return str_date;
		}else if(current_day>time&&time>yesterday){	
		}
		
		int day = (int) (time / (60 * 60 * 24));
		time -= day * (60 * 60 * 24);
		int hour = (int) (time / (60 * 60));
		time -= hour * 60 * 60;
		int minute = (int) (time / 60);
		int second = (int) (time - minute *  60);
		String format = null;*/
        return null;
    }

    /*
     * 把日期转成时间戳
     * */
    public static long gettempData(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
            Date parse = sdf.parse(date);
            return parse.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


}